package org.joshsacks.fishies.ui.gl;

import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_ENV_MODE;

import javax.microedition.khronos.opengles.GL10;

class TouchIcon {
  public enum IconType {
    LEFT_ARROW,
    RIGHT_ARROW,
    DOWN_ARROW,
    ROTATE,
  };
  
  static final int ICON_SIZE = 96;
  private static final long ANIMATE_TIME = 350;
  private static final float BASE_ALPHA = 0.5f;
  private static final float[] ROTATE_COORDS = 
    {1.0f, 0,
     0.0f, 0.0f, 
     0, 1.0f,
     1.0f, 1.0f,
    };
  private final int textureId;
  private final int x;
  private final IconType icon;
//  private final boolean flippedIcon;
//  private final boolean rotate;
  private long startAnimation;
  
  
  public TouchIcon(IconType icon, int textureId, int x) {
    this.icon = icon;
    this.textureId = textureId;
//    this.flippedIcon = flippedIcon;
//    this.rotate = rotate;
    this.x = x;
  }
  
  public void animate() {
    startAnimation = System.currentTimeMillis();
  }
 
  public void setPressed(boolean pressed) {
    if (pressed) {
      startAnimation = System.currentTimeMillis() + 200000;
    } else {
      startAnimation = 0;
    }
  }
  
  public void render(GL10 gl) {
    float delta = System.currentTimeMillis() - startAnimation;
    float alpha = BASE_ALPHA;
    if (delta < ANIMATE_TIME)  {
      alpha = 1.0f - BASE_ALPHA * delta / ANIMATE_TIME;
    } 
    if (alpha > 1.0f) {
      alpha = 1.0f;
    }
    
    SquareRenderer renderer = new SquareRenderer(gl);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    gl.glColor4f(1.0f, 1.0f, 1.0f, alpha);
    gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);  
    if (icon == IconType.DOWN_ARROW) {
      renderer.addRectangle(x, 40, ICON_SIZE, ICON_SIZE, ROTATE_COORDS);
    } else {
      boolean flippedIcon = icon == IconType.LEFT_ARROW;
      renderer.addSquare(x, 40, ICON_SIZE, flippedIcon); 
    }
    renderer.draw();      
  }
}